const { resolve } = require('path')
const { execFile } = require('child_process');
const { watchFile, unwatchFile } = require('fs')

const distUrl = resolve('./dist/dev/mp-weixin/')
// 微信开发这工具绝对地址
const weChatToolsUrl = 'D:/Program Files (x86)/Tencent/微信web开发者工具'

watchFile(`${distUrl}/app.js`, (cur, prv) => {
    if (cur.nlink == 1) {
        console.log("\n-  预览文件准备完毕,正在启动微信开发者工具...");
        console.log(`\n-  预览源文件地址:${distUrl}`);
        execFile("cli.bat", ["-o", distUrl], { cwd: weChatToolsUrl }, (error, stdout, stderr) => {
            console.log(stderr);
        });
        unwatchFile(`${distUrl}/app.js`)
    }
})